package com.jz.community.modulemine.ui.view;

import android.app.Dialog;
import android.content.Context;
import android.graphics.drawable.AnimationDrawable;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.jz.community.basecomm.utils.Preconditions;
import com.jz.community.modulemine.R;


/*
* 缓冲对话框的加载，显示，隐藏
* */
public class WaitingDialog {
    private Dialog mProgressDialog;
    private TextView loadingTv;
    private ImageView loadingIv;
    private AnimationDrawable mAnimation;

    public WaitingDialog(Context context) {
        this(context, context.getResources().getString(R.string.loading));
    }

    public WaitingDialog(Context context, String noteStr) {
        mProgressDialog = new Dialog(context, R.style.waitingDialog);
        LayoutInflater inflater = LayoutInflater.from(context);

        View v = inflater.inflate(R.layout.module_mine_waiting_dialog, null);
        loadingTv = (TextView) v.findViewById(R.id.loadingTv);
        loadingIv = (ImageView) v.findViewById(R.id.loadingIv);
        loadingTv.setText(noteStr);

        mAnimation = (AnimationDrawable) loadingIv.getBackground();

        mProgressDialog.setContentView(v);
        mProgressDialog.setCancelable(false);
        mProgressDialog.setCanceledOnTouchOutside(false);
    }

    public void setNotCancle() {
        mProgressDialog.setCancelable(false);
    }

    public synchronized void show() {
//		mProgressDialog.show();

        try {
            isCanceled = false;
            mHandler.sendEmptyMessageDelayed(0, 500);
        } catch (Exception e) {
        }
    }

    public synchronized void showNow() {
        try {
            isCanceled = false;
            if (!mProgressDialog.isShowing()) {
                mProgressDialog.show();
            }
        } catch (Exception e) {
        }
    }

    public synchronized void dismiss() {
        try {
            mHandler.removeMessages(0);
            isCanceled = true;
            if (mProgressDialog.isShowing()) {
                mProgressDialog.dismiss();
            }
        } catch (Exception e) {
        }
    }

    public void setContext(String text) {
        if (!Preconditions.isNullOrEmpty(text)) {
            loadingTv.setText(text);
        }
    }

    private boolean isCanceled;
    Handler mHandler = new Handler() {

        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            if (msg.what == 0) {
                if (isCanceled) {
                    if (mProgressDialog.isShowing()) {
                        mProgressDialog.dismiss();
                        mAnimation.stop();
                    }
                } else {
                    if (!mProgressDialog.isShowing()) {
                        mProgressDialog.show();
                        loadingIv.post(new Runnable() {
                            @Override
                            public void run() {
                                mAnimation.start();
                            }
                        });
                    }
                }
            }
        }

    };
}
